package com.twu.biblioteca.users;


import java.io.BufferedReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by mduemcke on 9/25/14.
 */
public class BibliotecaUsers {

    private BufferedReader reader;
    private List<User> registeredUsers;
    private User currentUser;

    public BibliotecaUsers(BufferedReader reader) {
        this.reader = reader;
        predifineUsers();
    }

    public boolean authenticateUser() {
        try {
            System.out.println("Please enter your library number (registered user: 123-4567)");
            String lib_number = reader.readLine();
            System.out.println("Please enter your password (registered user: passw0rd)");
            String password = reader.readLine();
            currentUser = verifyCredentials(lib_number, password);
            return currentUser != null;
        } catch(IOException ioe) {
            System.err.print(ioe.getStackTrace());
        }
        return false;
    }

    public void printUserInformation() {
        System.out.println("Your user information:");
        System.out.println("Name: " + currentUser.name);
        System.out.println("Phone number: " + currentUser.phone_number);
        System.out.println("Email: " + currentUser.email);
    }

    public void setReader(BufferedReader reader) {
        this.reader = reader;
    }

    private User verifyCredentials(String library_number, String password) {
        for(User u : registeredUsers) {
            if(u.library_number.equals(library_number) && u.password.equals(password)) {
                return u;
            }
        }
        System.out.println("Wrong user credentials");
        return null;
    }

    private void predifineUsers() {
        registeredUsers = new LinkedList<User>();
        registeredUsers.add(new User("123-4567", "passw0rd", "Ada", "111/2222", "ada@gmail.com"));
        registeredUsers.add(new User("234-5678", "passw0rd", "Bob", "222/3333", "bob@yahoo.com"));
    }

    private class User {
        private String library_number;
        private String password;
        private String name;
        private String phone_number;
        private String email;

        private User(String library_number, String password, String name, String phone_number, String email) {
            this.library_number = library_number;
            this.password = password;
            this.name = name;
            this.phone_number = phone_number;
            this.email = email;
        }
    }

}
